import { createSlice } from "@reduxjs/toolkit";
import type { GlobalState } from "../interface";

const initialState: GlobalState = {
  token: "",
  userInfo: "",
  // 组件大小
  assemblySize: "middle",
  // 语言
  language: "zh",
  // 主题配置
  themeConfig: {
    // 页脚
    footer: false,
    // 色弱模式(weak) || 灰色模式(gray)
    weakOrGray: "",
    // 深色模式
    isDark: false,
    // 面包屑导航
    breadcrumb: false,
    // 标签页
    tabs: false,
  },
  setAssemblySize: undefined
}

const useGlobalStore = createSlice({
  name: 'react-global',
  initialState,
  reducers: {
    setToken(state, action) {
      state.token = action.payload;
    },
    setUserInfo(state, action) {
      state.userInfo = action.payload;
    },
    setAssemblySize(state, action) {
      state.assemblySize = action.payload;
    },
    setLanguage(state, action) {
      state.language = action.payload;
    },
    setThemeConfig(state, action) {
      state.themeConfig = { ...state.themeConfig, ...action.payload };
    }
  }
})

const { setToken, setUserInfo, setAssemblySize, setLanguage, setThemeConfig } = useGlobalStore.actions;
export { setToken, setUserInfo, setAssemblySize, setLanguage, setThemeConfig };

export default useGlobalStore.reducer